package o;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import com.huawei.appgallery.essentialapp.api.EssentialAppDataBean;
import com.huawei.appgallery.essentialapp.api.EssentialAppDataBean.OneAppInfoBean;
import com.huawei.appgallery.foundation.card.base.bean.AppInfoBean;
import com.huawei.appgallery.foundation.ui.framework.cardframe.fragment.TaskFragment;
import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean;
import com.huawei.appgallery.serverreqkit.api.listener.IServerCallBack;
import com.huawei.appmarket.framework.startevents.bean.DistStartupResponse;
import com.huawei.appmarket.framework.startevents.bean.ZjbbSceneInfo;
import com.huawei.appmarket.service.essentialapp.bean.EssentialAppOperateAppBean;
import com.huawei.appmarket.service.essentialapp.bean.EssentialAppOperateBean;
import com.huawei.fastengine.fastview.startFastappEngine.bean.RpkInfo;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant.KeyAndValue;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import o.dge.d;
import o.ewm.n;

public class dil extends dif implements bqg {
    private static dil ॱ = new dil();
    private bqh ˊ = ((bqh) fho.ˏ().ˋ("EssentialApp").ॱ(bqh.class));
    private WeakReference<Activity> ˋ;
    private bqk ˎ;
    private long ˏ;

    static class c implements IServerCallBack {
        private final b ˋ;
        private WeakReference<Activity> ˏ;

        c(b bVar, Activity activity) {
            this.ˋ = bVar;
            this.ˏ = new WeakReference(activity);
        }

        public void prePostResult(BaseRequest o_cvu, ResponseBean responseBean) {
        }

        public void notifyResult(BaseRequest o_cvu, ResponseBean responseBean) {
            if (this.ˋ != null) {
                try {
                    if (responseBean.getResponseCode() == 0 && responseBean.getRtnCode_() == 0) {
                        Activity activity = (Activity) this.ˏ.get();
                        if (activity != null && dgm.ˎ(activity) == 5) {
                            eqo.ॱ().ˎ(dgm.ˎ(activity), new TaskFragment.b(o_cvu, responseBean));
                        }
                    }
                    this.ˋ.ˎ(ˋ(o_cvu, responseBean), dil.ॱ(responseBean));
                } catch (Exception e) {
                    Logger.ˋ("EssentialAppWrapper", "notifyResult error :" + e.toString());
                }
            }
        }

        private EssentialAppDataBean ˋ(BaseRequest o_cvu, ResponseBean responseBean) {
            if (!(responseBean instanceof DistStartupResponse)) {
                return null;
            }
            DistStartupResponse distStartupResponse = (DistStartupResponse) responseBean;
            distStartupResponse.ॱˎ();
            Logger.ˎ("EssentialAppWrapper", o_cvu.getMethod_() + " : returnCode :" + distStartupResponse.getResponseCode());
            return dil.ˏ().ॱ(distStartupResponse);
        }
    }

    static class e implements dgv {
        private final b ˎ;

        e(b bVar) {
            this.ˎ = bVar;
        }

        public void ˎ() {
            this.ˎ.ॱ();
        }
    }

    private dil() {
        this.ˊ.ˊ(this);
    }

    public static dil ˏ() {
        return ॱ;
    }

    private static int ॱ(ResponseBean responseBean) {
        int responseCode = responseBean.getResponseCode();
        if (responseCode != 0 || responseBean.getRtnCode_() == 0) {
            return responseCode;
        }
        return 1;
    }

    public boolean ˎ() {
        return this.ˊ != null && this.ˊ.ˊ();
    }

    public boolean ˋ() {
        return this.ˊ != null && this.ˊ.ʽ();
    }

    public boolean ˊ() {
        return this.ˊ != null && this.ˊ.ʻ();
    }

    public boolean ʽ() {
        return this.ˊ != null && this.ˊ.ˏ();
    }

    public void ʼ() {
        if (this.ˊ != null) {
            this.ˊ.ॱ();
        }
    }

    public boolean ॱॱ() {
        return this.ˊ != null && this.ˊ.ˎ();
    }

    public void ˎ(EssentialAppDataBean essentialAppDataBean) {
        if (this.ˊ != null) {
            this.ˊ.ˏ(essentialAppDataBean);
        }
    }

    public EssentialAppDataBean ॱ(DistStartupResponse distStartupResponse) {
        EssentialAppDataBean ʻॱ = distStartupResponse.ʻॱ();
        if (ʻॱ == null) {
            return ॱ(distStartupResponse.ᐝॱ());
        }
        return ʻॱ;
    }

    private EssentialAppDataBean ॱ(List<ZjbbSceneInfo> list) {
        EssentialAppDataBean essentialAppDataBean = new EssentialAppDataBean();
        if (!CollectCheck.isEmpty((List) list)) {
            ZjbbSceneInfo zjbbSceneInfo = (ZjbbSceneInfo) list.get(list.size() - 1);
            if (zjbbSceneInfo != null) {
                List ॱ = zjbbSceneInfo.ॱ();
                essentialAppDataBean.ˋॱ();
                if (!CollectCheck.isEmpty(ॱ)) {
                    essentialAppDataBean.ˎ(ˊ(ॱ));
                }
                essentialAppDataBean.ˋ(zjbbSceneInfo.ˊ());
                essentialAppDataBean.ˎ(1);
            }
        }
        return essentialAppDataBean;
    }

    private List<OneAppInfoBean> ˊ(List<OneAppInfoBean> list) {
        List<OneAppInfoBean> arrayList = new ArrayList();
        int i = 0;
        for (AppInfoBean appInfoBean : list) {
            int i2;
            if (appInfoBean == null || ˏ(appInfoBean)) {
                i2 = i;
            } else {
                arrayList.add(appInfoBean);
                i2 = i + 1;
            }
            if (i2 >= 18) {
                break;
            }
            i = i2;
        }
        return arrayList;
    }

    private boolean ˏ(@NonNull AppInfoBean appInfoBean) {
        return ((cdg) cdb.ॱ(cdg.class)).ॱॱ(appInfoBean.ˏॱ()) || (!HcridSession.ˋ().appsstoreGMSSupport() && 1 == appInfoBean.getGmsSupportFlag());
    }

    protected boolean ˊ(boolean z) {
        return z || (ॱॱ() && ˋॱ());
    }

    protected void ˊ(Activity activity) {
        if (activity instanceof FragmentActivity) {
            Context context = (FragmentActivity) activity;
            fjm ˏ = fjm.ˏ(fiu.ˊ().ˋ(context, fho.ˏ().ˋ("EssentialApp").ˎ("EssentialAppFragment")));
            try {
                FragmentTransaction ˊ = context.ˏॱ().ˊ();
                ˊ.ˏ(16908290, ˏ.ˏ());
                ˊ.ˋ();
            } catch (IllegalStateException e) {
                Logger.ˋ("EssentialAppWrapper", "Show EssentialAppFragment IllegalStateException!");
            } catch (ArrayIndexOutOfBoundsException e2) {
                Logger.ˋ("EssentialAppWrapper", "Show EssentialAppFragment ArrayIndexOutOfBoundsException!");
            }
            if (context instanceof did) {
                this.ˋ = new WeakReference(context);
            }
            dgh.onEvent(new d(context, n.ˋʼ).ˏ(KeyAndValue.NUMBER_01).ˋ());
        }
    }

    public boolean ॱ() {
        return true;
    }

    public void ॱ(Context context) {
        this.ˏ = System.currentTimeMillis();
        ʻ();
    }

    public void ˊ(Context context) {
        dgh.ˋ(context, "040103", this.ˏ);
        ͺ();
    }

    public void ˏ(Context context) {
        Activity activity = this.ˋ != null ? (Activity) this.ˋ.get() : null;
        if (activity instanceof did) {
            ((did) activity).ॱ("zjbbapps");
        }
        ˋ(activity);
    }

    public void ˏ(Activity activity, View view, List<OneAppInfoBean> list) {
        dgh.ॱ((Context) activity, "040106", KeyAndValue.NUMBER_01);
        EssentialAppOperateBean essentialAppOperateBean = new EssentialAppOperateBean();
        essentialAppOperateBean.ˎ(ᐝ());
        essentialAppOperateBean.ॱ("install");
        List arrayList = new ArrayList();
        dwe o_dwe = new dwe();
        if (!(list == null || list.isEmpty())) {
            for (AppInfoBean appInfoBean : list) {
                if (appInfoBean != null) {
                    ˏ(activity, appInfoBean);
                    o_dwe.ˏ(appInfoBean);
                    EssentialAppOperateAppBean essentialAppOperateAppBean = new EssentialAppOperateAppBean();
                    essentialAppOperateAppBean.ˋ(appInfoBean.ᐝ());
                    essentialAppOperateAppBean.ॱ(appInfoBean.ˊ());
                    essentialAppOperateAppBean.ˋ(appInfoBean.ˏ());
                    essentialAppOperateAppBean.ˎ(appInfoBean.ˋ());
                    arrayList.add(essentialAppOperateAppBean);
                }
            }
        }
        o_dwe.ˊ(activity, view);
        essentialAppOperateBean.ˊ(arrayList);
        ˊ(activity, essentialAppOperateBean);
    }

    public void ˏ(Activity activity, b bVar) {
        cvt ˎ = cvv.ˎ();
        ˎ.ˊ(1);
        ˎ.setServiceType_(dgm.ˎ(activity));
        cfw.ˎ(ˎ, new dgw(activity, o.dgw.c.INVOKE, new c(bVar, activity), new e(bVar)));
    }

    private void ˏ(Activity activity, AppInfoBean appInfoBean) {
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put(RpkInfo.APPID, appInfoBean.ᐝ());
        linkedHashMap.put("type", String.valueOf(1));
        linkedHashMap.put("service_type", String.valueOf(dgm.ˎ(activity)));
        linkedHashMap.put("detailid", appInfoBean.ॱˊ());
        dgh.ˋ("card_installbtn_click", linkedHashMap);
    }

    public int ᐝ() {
        return this.ˊ == null ? 0 : this.ˊ.ˋ();
    }

    public void ˊ(Activity activity, EssentialAppOperateBean essentialAppOperateBean) {
        try {
            cgo.ˋ("14", essentialAppOperateBean.toJson(), dgm.ˎ(activity));
        } catch (Exception e) {
            Logger.ˋ("EssentialAppWrapper", "json parse exception");
        }
    }

    public void ˋ(Activity activity) {
        EssentialAppOperateBean essentialAppOperateBean = new EssentialAppOperateBean();
        essentialAppOperateBean.ˎ(ᐝ());
        essentialAppOperateBean.ॱ("reject");
        ˊ(activity, essentialAppOperateBean);
    }

    private void ʻ() {
        if (this.ˋ != null) {
            Activity activity = (Activity) this.ˋ.get();
            if (activity != null) {
                cdk.ˊ(activity.getClass().getCanonicalName(), ॱˊ());
            }
        }
    }

    private void ͺ() {
        if (this.ˋ != null) {
            Activity activity = (Activity) this.ˋ.get();
            if (activity != null) {
                cdk.ˏ(activity.getClass().getCanonicalName(), ॱˊ());
            }
        }
    }

    private LinkedHashMap ॱˊ() {
        LinkedHashMap linkedHashMap = new LinkedHashMap(2);
        if (ᐝ() == 1) {
            linkedHashMap.put("user_agent", "app_recommend");
        } else if (ᐝ() == 2) {
            linkedHashMap.put("user_agent", "app_user_recommend");
        }
        Logger.ˎ("EssentialAppWrapper", "pageType=" + ᐝ());
        linkedHashMap.put("service_type", Integer.valueOf(dgm.ˎ(this.ˋ != null ? (Activity) this.ˋ.get() : null)));
        return linkedHashMap;
    }

    private boolean ˋॱ() {
        boolean z = true;
        if (this.ˎ != null) {
            z = this.ˎ.ˋ();
        }
        if (Logger.ˎ()) {
            Logger.ˏ("EssentialAppWrapper", "check result:" + z);
        }
        return z;
    }
}
